package com.vanvalt.payhui.modules.pay.api.business;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.vanvalt.payhui.modules.pay.api.command.impl.NewsCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.PageCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.RequestCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.ResponseCommand;
import com.vanvalt.payhui.modules.pay.api.util.ErrorCode;
import com.vanvalt.payhui.modules.pay.entity.News;
import com.vanvalt.payhui.modules.pay.service.NewsService;
import com.vanvalt.payhui.modules.pay.service.UserService;
import com.vanvalt.payhui.util.constat.Constant;
import com.vanvalt.payhui.util.json.JsonConversion;


/**
 * 资讯处理
 *
 */
public class NewsProcess extends BaseProcess {
	/**
	 *日志.
	 */
	private static Logger 				logger = Logger.getLogger(NewsProcess.class);
	/*private static HttpServletRequest	httpServletRequest	= ((ServletRequestAttributes) RequestContextHolder
			.getRequestAttributes()).getRequest();*/
	
	private UserService userService;
	private NewsService newsService;
	
	/**
	 * 构造方法.
	 */	
	public NewsProcess() {
		super();
	}

	/**
	 * @param newsService
	 */
	public NewsProcess(NewsService newsService){
		super();
		this.newsService = newsService;
	}
	
	/**
	 * @param userService
	 * @param newsService
	 */
	public NewsProcess(UserService userService, NewsService newsService){
		super();
		this.userService = userService;
		this.newsService = newsService;
	}

	/**
	 * 资讯列表
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public ResponseCommand list(PageCommand pageCommand, RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {
		
		// 分页参数
		pageCommand = initPagecommand(pageCommand, requestCommand);
		RowBounds rowBounds = new RowBounds(pageCommand.getPage().getOffset(), 
				pageCommand.getPage().getPageSize());
				
		NewsCommand newsCommand = new NewsCommand(requestCommand.getObject().toString());
		Map<String, Object> map = newsCommand.getMap();
		String uid = (String) map.get("uid");
        String currentPage = (String) map.get("currentPage");
        String pageSize = (String) map.get("pageSize");
		
		List<Map<String,Object>> resultList = Lists.newArrayList();
		
		if(map.isEmpty() && StringUtils.isBlank(uid) && StringUtils.isBlank(currentPage) && StringUtils.isBlank(pageSize)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	User user = userService.get(uid);
        	
        	if(user == null){
        		return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        	} else {
        		
        		/*Integer currentPages = Integer.parseInt(currentPage);
                Integer lineSize = Integer.parseInt(pageSize);*/
                
                News newsParams = new News();
                newsParams.setDelFlag(Constant.DEL_FLAG_NORMAL); // 正常
                newsParams.setLimit(rowBounds.getLimit());
                newsParams.setOffset(rowBounds.getOffset());
                
                /*Page<News> page = newsService.findPage(new Page<News>(currentPages, lineSize), newsParams); 
                List<News> newsList = page.getList();*/
                List<News> newsList = newsService.findListByPage(newsParams);
                
                String baseUrl = Global.getConfig("web.file.download.url");
                
                for(News news:newsList){
                	
                	Map<String,Object> resultMap = Maps.newHashMap();
                	
                	resultMap.put("title", news.getTitle());
                	String images = news.getImage();
                	
                	List<String> imageList = Lists.newArrayList();
                	if(images != null && !"".equals(images)){
                		imageList = this.getPictureMap(images, baseUrl);
                	} 
                	
                	/*String imgUrl = "";
                	if(image != null && !"".equals(image)){
                		imgUrl = baseUrl + image;
                	} */
                	resultMap.put("link", news.getLink());
                	resultMap.put("images", imageList);
                	resultMap.put("source", news.getSource());
                	resultMap.put("publishTime", DateUtils.formatDateTime(news.getCreateDate()));
                	
                	resultList.add(resultMap);
                }
         		
         		return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
         				JsonConversion.writeListJSON(resultList), responseCommand);
        	}
        }
	}

	private List<String> getPictureMap(String pictures, String baseUrl){
		
		List<String> resultList = Lists.newArrayList();
		
		if(pictures != null && !"".equals(pictures)){
			if(pictures.contains(Constant.SPLIT_FILE_NAME)){
				String[] pictureArray = pictures.split("\\|");
				
				for(String pic:pictureArray){
					if(pic.trim() != null && !"".equals(pic.trim())){
						pic = baseUrl + pic;
						resultList.add(pic);
					}
				}
			}
		}
		
		return resultList;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}


	
}
